/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Entity.TimeSet;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Cyrax
 */
@Stateless
public class TimeSetSessionBean implements TimeSetSessionBeanLocal {

    @PersistenceContext(unitName = "EasyTickets-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    @Override
    public List<TimeSet> listAllTimeSet() {
        List<TimeSet> ts = em.createNamedQuery("TimeSet.findAll").getResultList();
        return ts;
    }
    @Override
    public  List<TimeSet> listHours(Date date)
    {
         List<TimeSet> ts = null;

        ts = em.createQuery("SELECT t FROM TimeSet t WHERE t.dateRelease like :dateRelease").setParameter("dateRelease", "%" + date + "%").getResultList();
        System.out.println("dateRelease : " + date);


        return ts;
    }
    @Override
    public  List<TimeSet> SearchTimeset(String date)
    {
         List<TimeSet> ts = null;

        ts = em.createQuery("SELECT t FROM TimeSet t WHERE t.dateRelease like :dateRelease").setParameter("dateRelease", "%" + date + "%").getResultList();
        System.out.println("dateRelease : " + date);


        return ts;
    }

    @Override
    public void addTimeSet(TimeSet ts) {
        try {
            em.persist(ts);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateTimeSet(int tsId, String start, String end,Date releaseDate) {
        TimeSet ts = em.find(TimeSet.class, tsId);
        if (ts != null) {
            
            ts.setHoursStart(start);
            ts.setHoursEnd(end);
            ts.setDateRelease(releaseDate);
            em.persist(ts);
        } else {
            System.out.println("Not found tcId  to Update");
        }
    }

  

    @Override
    public void DeleteTimeSet(TimeSet ts) {

        if (ts != null) {
            em.remove(em.merge(ts));
        } else {
            System.out.println("Not found tkCat to delete");
        }
    }

    @Override
    public void upTimeSet(TimeSet ts) {
         TimeSet newTs = em.find(TimeSet.class, ts.getTsID());
        if (ts != null) {
         em.getTransaction().begin();
         newTs.setHoursEnd(ts.getHoursEnd());
         newTs.setHoursStart(ts.getHoursStart());
         em.getTransaction().commit();

        } else {
            System.out.println("Not found tcId  to Update");
        }
    }
}
